from django.test import TestCase
from django.core.urlresolvers import resolve
from lists.views import home_page
from django.http import HttpRequest
from django.http import HttpResponse
from django.template.loader import render_to_string

from lists.models import Item , List

# Create your tests here.
# class SmokeTest(TestCase):
#    def test_bad_maths(self):
#        self.assertEqual(1+1,3)

#class HomePageTest(TestCase):
#
#    def test_root_url_resolves_to_home_page_view(self):
#        found = resolve('/')
#        self.assertEqual(found.func,home_page)

#    def test_home_page_returns_correct_html(self):
#        request = HttpRequest()
#        response = home_page(request)
#        excepted_html = render_to_string('home.html')
#        self.assertEqual(response.content.decode(),excepted_html)  

#    def test_home_page_can_save_a_POST_request(self):#
#        request = HttpRequest()
#        request.method='POST'
#        request.POST['item_text'] = 'A new list item'

#        response = home_page(request)

#        self.assertEqual(Item.objects.count(),1)
#        new_item = Item.objects.first()
#        self.assertEqual(new_item.text,'A new list item')

        #self.assertIn('A new list item',response.content.decode()) 
        #expected_html = render_to_string(
        #    'home.html',
        #    {'new_item_text':'A new list item'}
        #)
        #self.assertEqual(response.content.decode(),expected_html)
    
#    def test_home_page_redirects_after_POST(self):
#        request = HttpRequest()
#        request.method = 'POST'
#        request.POST['item_text'] = 'A new list item'

#        response = home_page(request)

#        self.assertEqual(response.status_code,302)
#        self.assertEqual(response['location'],'/lists/the-only-list-in-the-world/')

#class ItemModelTest(TestCase):
class ListAndItemModelsTest(TestCase):

    def test_saving_and_retrieving_item(self):
        list_ = List()
        list_.save()

        first_item = Item()
        first_item.text = 'The first(ever) list item'
        first_item.list = list_
        first_item.save()

        second_item = Item()
        second_item.text = 'Item the second'
        second_item.list = list_
        second_item.save()

        saved_items = Item.objects.all()
        self.assertEqual(saved_items.count(),2)

        first_saved_item = saved_items[0]
        second_saved_item = saved_items[1]
        self.assertEqual(first_saved_item.text,'The first(ever) list item')
        self.assertEqual(first_saved_item.list,list_)
        self.assertEqual(second_saved_item.text,'Item the second')
        self.assertEqual(second_saved_item.list,list_)

#class HomePageTest1(TestCase):
    
    #def test_home_page_only_saves_items_when_necessary(self):
        #request = HttpRequest()
        #home_page(request)
        #self.assertEqual(Item.objects.count(),0)

    #def test_home_page_display_all_list_items(self):
        #Item.objects.create(text='itemey 1')
        #Item.objects.create(text='itemey 2')

        #request = HttpRequest()
        #response = home_page(request)

        #self.assertIn('itemey 1',response.content.decode())
        #self.assertIn('itemey 2',response.content.decode())

class ListViewTest(TestCase):

    def test_users_list_template(self):
        list_ = List.objects.create()
        response = self.client.get('/lists/%d/' % (list_.id,))
        self.assertTemplateUsed(response,'list.html')

    def test_displays_only_items_for_that_list(self):
        correct_list = List.objects.create()
        #list_ = List.objects.create()
        Item.objects.create(text='itemey 1',list=correct_list)
        Item.objects.create(text='itemey 2',list=correct_list)
        other_list = List.objects.create()
        Item.objects.create(text='other list item 1',list=other_list)
        Item.objects.create(text='other list item 2',list=other_list)

        #response = self.client.get('/lists/the-only-list-in-the-world/')
        response = self.client.get('/lists/%d/' % (correct_list.id))

        self.assertContains(response,'itemey 1')
        self.assertContains(response,'itemey 2')
        self.assertNotContains(response,'other list item 1')
        self.assertNotContains(response,'other list item 2')
    
    def test_passes_correct_list_to_template(self):
        other_list = List.objects.create()
        correct_list = List.objects.create()
        response = self.client.get(
            '/lists/%d/' % (correct_list.id,)
        )
        self.assertEqual(response.context['list'],correct_list)

class NewListTest(TestCase):


    def test_saving_a_POST_request(self):
        self.client.post(
            '/lists/new',
            data={'item_text':'A new list item'}
        )
        self.assertEqual(Item.objects.count(),1)
        new_item = Item.objects.first()
        self.assertEqual(new_item.text,'A new list item')
        
        #request = HttpRequest()
        #request.method='POST'
        #request.POST['item_text'] = 'A new list item'

        #response = home_page(request)

        #self.assertEqual(Item.objects.count(),1)
        #new_item = Item.objects.first()
        #self.assertEqual(new_item.text,'A new list item')
        
        #self.assertIn('A new list item',response.content.decode()) 
        #expected_html = render_to_string(
        #    'home.html',
        #    {'new_item_text':'A new list item'}
        #)
        #self.assertEqual(response.content.decode(),expected_html)
    
    def test_home_page_redirects_after_POST(self):
        response = self.client.post(
            '/lists/new',
            data={'item_text':'A new list item'}
        )
        new_list = List.objects.first()
        self.assertRedirects(response,'/lists/%d/' % (new_list.id,))
        #self.assertEqual(response.status_code,302)
        #self.assertEqual(response['location'],'/lists/the-only-list-in-the-world/')
        
        #request = HttpRequest()
        #request.method = 'POST'
        #request.POST['item_text'] = 'A new list item'

        #response = home_page(request)

        #self.assertEqual(response.status_code,302)
        #self.assertEqual(response['location'],'/lists/the-only-list-in-the-world/')
        

class NewItemTest(TestCase):
    def test_can_save_a_POST_request_to_an_existing_list(self):
        other_list = List.objects.create()
        correct_list = List.objects.create()

        self.client.post(
            '/lists/%d/add_item' % (correct_list.id),
            data={'item_text':'A new item for an existing list'}
        )
        self.assertEqual(Item.objects.count(),1)
        new_item = Item.objects.first()
        self.assertEqual(new_item.text,'A new item for an existing list')
        self.assertEqual(new_item.list,correct_list)

    def test_redirects_to_list_view(self):
        other_list = List.objects.create()
        corrent_list = List.objects.create()

        response = self.client.post(
            '/lists/%d/add_item' % (corrent_list.id),
            data={'item_text':'A new item for an existing list'}
        )
        self.assertRedirects(response,'/lists/%d/' % (corrent_list.id))